// const CryptoJS = require('crypto-js');
import {JSEncrypt} from 'jsencrypt';
import CryptoJS from "crypto-js";


// RSA 生成rsakey
export function get_raskey() {
    let rsa = new JSEncrypt();

    return {
        pubkey: rsa.getPublicKey(),
        prikey: rsa.getPrivateKey()
    }
}


// RSA 解密方法
export function rsa_decrypt(ciphertext, prikey) {
    let decrypt = new JSEncrypt();
    decrypt.setPrivateKey(prikey);
    return decrypt.decrypt(ciphertext);
}

// RSA 加密方法
export function rsa_encrypt(plaintext, pubkey) {
    let encrypt = new JSEncrypt();
    encrypt.setPublicKey(pubkey);
    return encrypt.encrypt(plaintext);
}


// DES 解密
export function des_decrypt(ciphertext, key) {
    let b2a_key = CryptoJS.enc.Base64.parse(key)
    let dec_data = CryptoJS.DES.decrypt(
        {
            ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
        },
        b2a_key,
        {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        }).toString(CryptoJS.enc.Utf8);
    return JSON.parse(dec_data);
}


// DES 加密
// export function des_encrypt(plaintext, key) {
//
// }